package com.sh.data.engine.domain.util;

import com.sap.db.jdbc.HanaClob;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.Reader;
import java.sql.SQLException;

/**
 * @author tinglan.ys
 * @description: Clob转化工具类
 * @date 2022/4/29 下午3:30
 */
@Slf4j
public class ClobUtil {

    /**
     * @description : hanaClob转化为String
     * @author : tinglan.ys
     * @date : 2022/4/29
     */
    public static String hanaClobToString(final HanaClob clob) {

        if (clob == null) {
            return null;
        }

        StringBuffer sb = new StringBuffer();

        try (Reader is = clob.getCharacterStream();
             BufferedReader br = new BufferedReader(is)) {
            String temp;

            while ((temp = br.readLine()) != null) {

                sb.append(temp);
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        return sb.toString();
    }

    public static String hanaClobToString2(final HanaClob clob) throws SQLException {

        if (clob == null) {
            return null;
        }

        return clob.getSubString(1L, Math.toIntExact(clob.length()));
    }
}
